#define BLINKER_WIFI
#include <Blinker.h>


char auth[] = "65da5fa1deb5";
char ssid[] = "xiaoyang";
char pswd[] = "12345678";


BlinkerButton Button7("W");
BlinkerButton Button8("S");
BlinkerButton Button9("A");
BlinkerButton Button10("D");
BlinkerButton Button15("STOP");

//定义蓝牙，按钮，摇杆
int RIGHT_IN3 = 4;
int RIGHT_IN4 = 5;

int LEFT_IN1= 12;
int LEFT_IN2 = 13;
void Button7_callback(const String & state)
{
   if(state == "tap")
   {
    FRONT();
   }
}
void Button8_callback(const String & state)
{
   if(state == "tap")
   {
    BEHIND();
   }
}
void Button9_callback(const String & state)
{
   if(state == "tap")
   {
    LEFT();
   }
}
void Button10_callback(const String & state)
{
   if(state == "tap")
   {
    RIGHT();
   }
}

void Button15_callback(const String & state)
{
   if(state == "tap")
   {
    stopMotors();
   }
}

void FRONT()
{
  // 右侧电机
  digitalWrite(RIGHT_IN3, HIGH);
  digitalWrite(RIGHT_IN4, LOW);
 

  // 左侧电机
  digitalWrite(LEFT_IN1, HIGH);
  digitalWrite(LEFT_IN2, LOW);

}
void BEHIND()
{
   // 右侧电机
  digitalWrite(RIGHT_IN3, LOW);
  digitalWrite(RIGHT_IN4, HIGH);

  // 左侧电机
  digitalWrite(LEFT_IN1, LOW);
  digitalWrite(LEFT_IN2, HIGH);


}
void LEFT()
{
  // 右侧电机
  digitalWrite(RIGHT_IN3, HIGH);
  digitalWrite(RIGHT_IN4, LOW);


  // 左侧电机
  digitalWrite(LEFT_IN1,LOW);
  digitalWrite(LEFT_IN2, HIGH);

 
}
void RIGHT()
{
   // 右侧电机
  digitalWrite(RIGHT_IN3, LOW);
  digitalWrite(RIGHT_IN4, HIGH);


 // 左侧电机
  digitalWrite(LEFT_IN1, HIGH);
  digitalWrite(LEFT_IN2, LOW);


}
void stopMotors()
{
 // 右侧电机
  digitalWrite(RIGHT_IN3, LOW);
  digitalWrite(RIGHT_IN4, LOW);

  // 左侧电机
  digitalWrite(LEFT_IN1, LOW);
  digitalWrite(LEFT_IN2, LOW);
}


void setup() 
{
  // 设置所有引脚为输出模式
  pinMode(RIGHT_IN3, OUTPUT);
  pinMode(RIGHT_IN4, OUTPUT);
  
  pinMode(LEFT_IN1, OUTPUT);
  pinMode(LEFT_IN2, OUTPUT);



  // 初始化Blinker
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    BLINKER_DEBUG.debugAll();
  Blinker.begin(auth, ssid, pswd);
  Button7.attach(Button7_callback);
  Button8.attach(Button8_callback);
  Button9.attach(Button9_callback);
  Button10.attach(Button10_callback);
  Button15.attach(Button15_callback);
}

void loop() 
{
  // 处理Blinker事件
  Blinker.run();
}